/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.LogUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.ToastUtil;
import com.xuexiang.xuidemo.view.DirectionLaytionTextview;

/**
 * 导航栏
 *
 * @since 2021-03-22
 */
public class TitleBarAbilityslice extends AbilitySlice implements DirectionLaytionTextview.CallbackClick {
    private String tag = "TitleBarAbilityslice";
    private DirectionLaytionTextview titleone;
    private DirectionLaytionTextview titletwo;
    private DirectionLaytionTextview titlethere;
    private DirectionLaytionTextview titlefour;
    private Image imgbace;
    private Context context;
    private final int anInt1 = 1;
    private final int anInt2 = 2;
    private final int anInt3 = 3;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_titlebar);
        context = this;
        initview();
    }

    private void initview() {
        titleone = (DirectionLaytionTextview) findComponentById(ResourceTable.Id_Titleone);
        titleone.setright(anInt1, 0); // 全部隐藏
        titleone.setclick(this::setComponent);

        titletwo = (DirectionLaytionTextview) findComponentById(ResourceTable.Id_Titletwo);
        titletwo.setLeftLayout(anInt1, 0);
        titletwo.setCentent(anInt2);
        titletwo.setclick(new DirectionLaytionTextview.CallbackClick() {
            @Override
            public void setComponent(Component component) {
                switch (component.getId()) {
                    case ResourceTable.Id_img1: // 最左边的图片
                    case ResourceTable.Id_DependenBace:
                        ToastUtil.showToast(getContext(), "点击返回");
                        break;
                    case ResourceTable.Id_img2: // 最右边的图片
                        ToastUtil.showToast(getContext(), "点击更多!");
                        break;
                    case ResourceTable.Id_textcent: // 中间的大标题
                        ToastUtil.showToast(getContext(), "点击标题");
                        break;
                    default:
                        break;
                }
            }
        });

        titlethere = (DirectionLaytionTextview) findComponentById(ResourceTable.Id_Titlethere);
        titlethere.setLeftLayout(anInt2, ResourceTable.Media_ic_navigation_back_white);
        titlethere.setCentent(anInt1);
        titlethere.setright(anInt3, 0);
        titlethere.setclick(this::setComponent);

        titlefour = (DirectionLaytionTextview) findComponentById(ResourceTable.Id_Titlefour);
        titlefour.setLeftLayout(anInt3, 0);
        titlefour.setCentent(anInt1);
        titlefour.setright(anInt2, ResourceTable.Media_ic_navigation_more);
        titlefour.setclick(this::setComponent);

        imgbace = (Image) findComponentById(ResourceTable.Id_bace);
        imgbace.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate(); // 关闭当前的ability
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
        LogUtils.info(tag, "onActive");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        LogUtils.info(tag, "onForeground");
    }

    @Override
    public void setComponent(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_img1: // 最左边的图片
            case ResourceTable.Id_DependenBace:
                ToastUtil.showToast(getContext(), "点击返回");
                break;
            case ResourceTable.Id_textright: // 最右边的字体
                ToastUtil.showToast(getContext(), "点击更多!");
                break;
            case ResourceTable.Id_img2: // 最右边的图片
                ToastUtil.showToast(getContext(), "点击菜单!");
                break;
            case ResourceTable.Id_textcent: // 中间的大标题
                ToastUtil.showToast(getContext(), "点击标题");
                break;
            default:
                break;
        }
    }
}
